1、numpy
2、pandas
3、matplotlib
数组跟列表,列表可以存储任意类型的数据,而数组只能存储一种类型数据
In [12]:
import array
In [14]:
a = array.array('i', range(10))
In [19]:
# 数据类型必须统一
a[1] = 's'
In [17]:
a
Out[17]:
In [21]:
import numpy as np
In [25]:
a_list = list(range(10))
b = np.array(a_list)
type(b)
Out[25]:
生成数组
In [31]:
a = np.zeros(10, dtype=int)
print(type(a))
# 查看数组类型
a.dtype
Out[31]:
In [33]:
a = np.zeros((4,4), dtype=int)
print(type(a))
# 查看数组类型
print(a.dtype)
a
Out[33]:
In [34]:
np.ones((4,4), dtype=float)
Out[34]:
In [35]:
np.full((3,3), 3.14)
Out[35]:
In [36]:
a
Out[36]:
In [37]:
np.zeros_like(a)
Out[37]:
In [38]:
np.ones_like(a)
Out[38]:
In [40]:
np.full_like(a, 4.12, dtype=float)
Out[40]:
In [72]:
print(random.randint(5,10))
print(random.random())
In [74]:
np.random.random((3,3))
Out[74]:
In [83]:
# 经常会用到
np.random.randint(0,10, (5,5))
Out[83]:
In [87]:
list(range(0,10,2))
Out[87]:
In [92]:
np.arange(0,3,2)
Out[92]:
In [94]:
# 经常用到
np.linspace(0, 3, 10)
Out[94]:
In [97]:
# n维的单位矩阵
np.eye(5)
Out[97]:
Data type | Description |
---|---|
bool_ |
Boolean (True or False) stored as a byte |
int_ |
Default integer type (same as C long ; normally either int64 or int32 ) |
intc |
Identical to C int (normally int32 or int64 ) |
intp |
Integer used for indexing (same as C ssize_t ; normally either int32 or int64 ) |
int8 |
Byte (-128 to 127) |
int16 |
Integer (-32768 to 32767) |
int32 |
Integer (-2147483648 to 2147483647) |
int64 |
Integer (-9223372036854775808 to 9223372036854775807) |
uint8 |
Unsigned integer (0 to 255) |
uint16 |
Unsigned integer (0 to 65535) |
uint32 |
Unsigned integer (0 to 4294967295) |
uint64 |
Unsigned integer (0 to 18446744073709551615) |
float_ |
Shorthand for float64 . |
float16 |
Half precision float: sign bit, 5 bits exponent, 10 bits mantissa |
float32 |
Single precision float: sign bit, 8 bits exponent, 23 bits mantissa |
float64 |
Double precision float: sign bit, 11 bits exponent, 52 bits mantissa |
complex_ |
Shorthand for complex128 . |
complex64 |
Complex number, represented by two 32-bit floats |
complex128 |
Complex number, represented by two 64-bit floats |
In [99]:
# 嵌套列表的元素访问
var = [[1,2,3], [3,4,5], [5,6,7]]
var[0][0]
Out[99]:
In [109]:
# 数组中元素的访问
a = np.array(var)
a[-1][0]
Out[109]:
In [111]:
a
Out[111]:
In [114]:
# 这两种访问方式是等价的
a[2, 0], a[2][0]
Out[114]:
In [120]:
# 数组切片
a[:2, :2]
Out[120]:
In [128]:
# 同上边的方式是不等价的
a[:2][:2]
Out[128]:
In [129]:
a
Out[129]:
In [136]:
# 维度
print(a.ndim)
# shape
print(a.shape)
# size
print(a.size)
# dtype
print(a.dtype)
# a.itemsize
print(a.itemsize)
# nbytes
print(a.nbytes)
In [138]:
a = np.array(list(range(10)))
a
Out[138]:
In [143]:
print(a + 10)
print(a - 10)
print(a * 100)
In [150]:
a = np.full((3,3), 1.0, dtype=float)
a + 10 # 等价于 np.add(a, 10)
Out[150]:
Operator | Equivalent ufunc | Description |
---|---|---|
+ |
np.add |
Addition (e.g., 1 + 1 = 2 ) |
- |
np.subtract |
Subtraction (e.g., 3 - 2 = 1 ) |
- |
np.negative |
Unary negation (e.g., -2 ) |
* |
np.multiply |
Multiplication (e.g., 2 * 3 = 6 ) |
/ |
np.divide |
Division (e.g., 3 / 2 = 1.5 ) |
// |
np.floor_divide |
Floor division (e.g., 3 // 2 = 1 ) |
** |
np.power |
Exponentiation (e.g., 2 ** 3 = 8 ) |
% |
np.mod |
Modulus/remainder (e.g., 9 % 4 = 1 ) |
In [155]:
a = np.linspace(0, np.pi, 5)
b = np.sin(a)
print(a)
print(b)
In [169]:
# 求和
print(sum([1,2,3,4,5,6]))
# 数组一维求和
a = np.full(10, 2.3)
print(sum(a))
# 数组多维求和
a = np.array([[1,2],[3,4]])
print(sum(a))
# np.sum 求和
np.sum(a)
np.sum(a, axis=1)
np.max(a, axis=1)
Out[169]:
In [172]:
n = np.random.rand(10000)
%timeit 代码 ; 此方法来判断程序的执行效率
In [174]:
%timeit sum(n)
In [175]:
%timeit np.sum(n)
In [178]:
a = np.array(range(10))
a
Out[178]:
In [177]:
a > 3
Out[177]:
In [180]:
a != 3
Out[180]:
In [181]:
a == a
Out[181]:
Operator | Equivalent ufunc | Operator | Equivalent ufunc | |
---|---|---|---|---|
== |
np.equal |
!= |
np.not_equal |
|
< |
np.less |
<= |
np.less_equal |
|
> |
np.greater |
>= |
np.greater_equal |
In [183]:
np.all(a>-1)
Out[183]:
In [184]:
np.any(a>-1)
Out[184]:
In [187]:
a = np.full((2,10), 1, dtype=float)
a
Out[187]:
In [196]:
a.reshape(4, 5)
Out[196]:
In [198]:
l = [
[1,2,3],
[34,12,4],
[32,2,33]
]
a = np.array(l)
a
Out[198]:
In [206]:
np.sort(a)
a.sort(axis=0)
a
Out[206]:
In [208]:
a = np.array([1, 2, 3])
b = np.array([[0, 2, 4], [1, 3, 5]])
In [215]:
# 按行去连接
np.concatenate([b,b,b], axis=0)
Out[215]:
In [214]:
# 按列去连接
np.concatenate([b,b,b], axis=1)
Out[214]:
In [ ]: